package com.mq;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.EventListener;
import java.util.concurrent.TimeoutException;

/**
 * @Author:zxh
 * @Description: Rabbit MQ
 * @Date: 2020/9/6
 * @Modified By:
 */
public class Producer {
    static  String EXCHANGE_NAME = "logs";
    public void producter() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        //创建交换器
        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");
        //发布一条消息
     //   channel.basicPublish("","hello",null,);
    }


    public void  Receive() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");
        String queue = channel.queueDeclare().getQueue();
        channel.queueBind(queue,EXCHANGE_NAME,"");

        Consumer consumer =  new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope,AMQP.BasicProperties properties,byte[] bytes) throws UnsupportedEncodingException {
                String str = new String(bytes, "utf-8");
                System.out.println("receive"+str+"");
            }
        };
        channel.basicConsume(queue,true,consumer);
    }

    /*channel.basicPublish(EXCHANGE_TOPICS_INFORM, "inform.email", null,

            message.getBytes());*/
    /*channel.basicPublish(EXCHANGE_ROUTING_INFORM, QUEUE_INFORM_SMS, null,

                        message.getBytes());*/

}
